package lib

func init() {
	Probs = append(Probs, Problem{
		Num:         59,
		Discription: "螺旋矩阵",
		Level:       2,
		Labels: map[string]int{
			"矩阵": 1,
		},
	})
}

func GenerateMatrix(n int) [][]int {
	topEdge := 0
	bottomEdge := n - 1
	leftEdge := 0
	rightEdge := n - 1
	k := 1
	res := make([][]int, n)
	for i := range res {
		res[i] = make([]int, n)
	}

	for k <= n*n {
		for j := leftEdge; j <= rightEdge && k <= n*n; j++ {
			res[topEdge][j] = k
			k++
		}
		topEdge++

		for i := topEdge; i <= bottomEdge && k <= n*n; i++ {
			res[i][rightEdge] = k
			k++
		}
		rightEdge--

		for j := rightEdge; j >= leftEdge && k <= n*n; j-- {
			res[bottomEdge][j] = k
			k++
		}
		bottomEdge--

		for i := bottomEdge; i >= topEdge && k <= n*n; i-- {
			res[i][leftEdge] = k
			k++
		}
		leftEdge++
	}

	return res
}